#ifndef RISING_STAR_OBJECT_H
#define RISING_STAR_OBJECT_H
#include <string>
#include <map>
#include <list>
#include <iostream>
#include "Config.h"
#include "Database.h"

using namespace std;

#define SEM_SN_LENGTH			7
#define SEM_SERIAL_LENGTH		3

#define SEM_ID_LENGTH			(SEM_SN_LENGTH + SEM_SERIAL_LENGTH)

#define OBJECT_CODE_LENGTH		2
//#define PROVINCE_CODE_LENGTH	3
//#define COUNTY_CODE_LENGTH		3
//#define AREA_CODE_LENGTH		5
//#define SERIAL_CODE_LENGTH		7
#define OBJECT_PHONE_LENGTH			11
#define OBJECT_SERIAL_LENGTH		8


//对象ID，共20个字节
//#define OBJECT_ID_LENGTH	(OBJECT_CODE_LENGTH + PROVINCE_CODE_LENGTH +COUNTY_CODE_LENGTH + AREA_CODE_LENGTH + SERIAL_CODE_LENGTH)	
#define OBJECT_ID_LENGTH (OBJECT_CODE_LENGTH + OBJECT_PHONE_LENGTH + OBJECT_SERIAL_LENGTH)

#define DEBUG_POSTION	__FILE__<<":"<<__FUNCTION__<<"["<<__LINE__<<"] "
namespace RisingStar
{
	
	typedef struct
	{
		map<string,string> property;
		map<string,string> tables;
	}TDatabaseParam;
	/*
	nValues 可取的值
	1、对象类型
	2、数据表
	
	*/
	
	typedef struct
	{
		map<string,string> property;		//属性值：对象ID(objectID)，请求量(requestID)
		//map<string,string> spArr;		//信号量值，key是信号ID，value是值
		map<int, map<int,map<string,string> > > spArr; //信号类型顺序号，信号序号,信号属性,包括ID,值(value),时间(time)
	}TObjectProperty;
	
	typedef struct
	{
		map<string,string> property;
		
		map<string,map<string,string> > sems;	//map<必要的信号量/其它的信号量, map<信号ID,信号ID> > 
	}TObjectCtrlCfg;	//对象控制配置
	// 对象基类
	class CObject
	{
		public:
		typedef boost::shared_ptr<CObject> ObjectPtr;
	protected:		

		TObjectProperty mObjectProperty;		//对象的属性,由报文传进来的参数决定的
		map<string,TObjectCtrlCfg> mObjectCtrlCfgs;		//对象控制属性，由配置属性进行控制
		map<string,string> nValues;		//内部保留的参数
		
		list<TObjectProperty> mOutObjList;	//经过解析返回的结果
		
		bool AnalysisDatabaseKey();
		
		//分析对象请求传进来的参数，进行过滤等操作
		bool AnalysisObject(map<string,map<string,string> > & tables,map<string,map<string,string> > & extTable);
		//分析对象查询传进来的参数，进行过滤等操作
		//map<string,map<string,string> > AnalysisInquiryObject();
		bool AnalysisInquiryObject(map<string,map<string,string> >& table,map<string,map<string,string> >& extTable );
	public:

		CObject(TObjectProperty in);
		CObject();
		virtual ~CObject();
		
		bool Go();
		
		const list<TObjectProperty>& Out();

		//获取该对象的编码属性
		//数据库关键字 	1\用于对数据库连接参数的关键字
		//对象类型编码	1\01~12,含义参照字典表
		//对象类型		1\用户:注册用户,全网唯一，编码以手机号作为编码
		//				2\车位:
		//				3\区域
		//				4\汽车
		//				5\组
		//				6\平台
		//				7\订单
		//手机号码		1\用户的手机号码，即注册码
		//对象索引		1\该对象对应的索引
		//对象序号		1\对象在用户下的顺序号
		//对象编码		1\对象实际对应的完整编码
		
		const string Code(string codeType);

		//获取制定信号类型的信号列表
		const map<string,map<string,string > > GetSemTypeIDs(int semType);
		string GetObjectType();	//获取对象类型
		int CodeInt(string codeType);	//获取对象类型
		virtual ObjectPtr Copy();
		virtual CObject& operator = (const CObject &d);
		bool LoadPropertCfg(string path);	//加载配置文件目录,一般在初始化的时候使用
		bool LoadDicSPID(string request,string replyType,string semID);
		bool AttachObjectProperty(TObjectProperty in);
		bool AttachDatabase(DatabasePtr& pData);
		
		string GetProperty(string key);
		
		static bool ObjectCallbackSqlAddOrEdit(CObject* obj);//增加修改
		static bool ObjectCallbackSqlAdd(CObject* obj);		//增加
		static bool ObjectCallbackSqlEdit(CObject* obj);	//修改
		static bool ObjectCallbackSqlDel(CObject* obj);		//删除
		static bool ObjectCallbackSqlSearch(CObject* obj);	//搜索
		static bool ObjectCallbackSqlInquiry(CObject* obj);	//查询
	private:
		DatabasePtr mDatabasePtr;
		bool AnalysisID(string id);		
		bool InitObjCallback();
	};
	typedef bool (* DEF_REQUEST_CALLBACK)(CObject* obj); 
	
	
};

#endif
